package com.zlll.winner.controller.activity;

import com.alibaba.fastjson.JSONObject;
import com.zlll.winner.business.model.activity.ActivityManagement;
import com.zlll.winner.business.service.activity.IActivityApplyExternalService;
import com.zlll.winner.business.service.activity.IActivityApplyService;
import com.zlll.winner.business.service.activity.IActivityManagementService;
import com.zlll.winner.common.BaseController;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/activity")
public class ActivityController extends BaseController {

    @Reference
    private IActivityManagementService managementService;

    @Reference
    private IActivityApplyService applyService;

    @Reference
    private IActivityApplyExternalService externalService;

    @PostMapping("getPage")
    public Object getPage(@RequestBody JSONObject data) {
        String title = data.getString("title");
        String cityCode = data.getString("cityCode");

        Long startTime = data.getLong("startTime");
        Long endTime = data.getLong("endTime");

        Integer status = data.getInteger("status");

        Integer pageNo = data.getInteger("pageNo") == null ? 1 : data.getInteger("pageNo");
        Integer pageSize = data.getInteger("pageSize") == null ? 20 : data.getInteger("pageSize");

        return managementService.getActivityManagementPage(title, cityCode, status, startTime, endTime, pageNo, pageSize);
    }

    @PostMapping("add")
    public Object add(@RequestBody ActivityManagement activityManagement) {
        return managementService.add(activityManagement, getUserId());
    }

    @PostMapping("edit")
    public Object edit(@RequestBody ActivityManagement activityManagement) {
        System.out.println(JSONObject.toJSONString(activityManagement));
        if (activityManagement.getStatus() != null && activityManagement.getStatus() == 0){
            activityManagement.setStatus(null);
        }
        return managementService.edit(activityManagement, getUserId());

    }

    @PostMapping("updateStatus")
    public Object updateStatus(@RequestBody JSONObject data) {
        Long id = data.getLong("id");
        Assert.notNull(id, "活动ID不能为空");

        Integer status = data.getInteger("status");
        return managementService.updateStatus(id, status, getUserId());
    }

    @PostMapping("delete")
    public Object delete(@RequestBody JSONObject data) {
        Long id = data.getLong("id");
        Assert.notNull(id, "活动ID不能为空");

        return managementService.deleteActivityById(id, getUserId());

    }

    @GetMapping("edit/get/{id}")
    public Object getActivityInfo(@PathVariable Long id) {
        return managementService.getActivityInfo(id);

    }

    @PostMapping("getApplyInfoList")
    public Object getApplyInfo(@RequestBody JSONObject data) {
        String cityCode = data.getString("cityCode");
        String name = data.getString("name");
        String telphone = data.getString("telphone");
        Long activityId = data.getLong("activityId");
        Long startTime = data.getLong("startTime");
        Long endTime = data.getLong("endTime");
        Integer pageNo = data.getInteger("pageNo") == null ? 1 : data.getInteger("pageNo");
        Integer pageSize = data.getInteger("pageSize") == null ? 20 : data.getInteger("pageSize");
        return applyService.getActivityApplyByActivityId(activityId,cityCode, name, telphone, startTime, endTime, pageNo, pageSize);
    }

    @PostMapping("updateApplyStatus")
    public Object updateApplyStatus(@RequestBody JSONObject data){
        Long id = data.getLong("id");
        Integer status = data.getInteger("status");

        return applyService.updateStatus(id,status,getUserId());
    }

    @PostMapping("getExternalByActivityId")
    public Object getExternalByActivityId(@RequestBody JSONObject data){
        Long activityId = data.getLong("activityId");
        return externalService.getExternalByActivityId(activityId);
    }

    @PostMapping("updateActivityApply")
    public Object updateActivityApply(@RequestBody JSONObject data){
        Long id = data.getLong("id");
        String name = data.getString("name");
        String telphone = data.getString("telphone");
        Integer num = data.getInteger("num");
        String externalInfo = data.getString("externalInfo");
        return applyService.updateActivityApply(id,name,telphone,num,externalInfo,getUserId());
    }
}
